package com.copc.mq.listener;

import com.copc.mq.bean.SysConstant;
import com.copc.service.CopcService;
import com.rabbitmq.client.Channel;
import java.io.IOException;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 接收消息监听类
 * @author yuanbin
 * @version 1.0
 */
@Component
@EnableRabbit
@Slf4j
public class ReceiveMsgListener {

    @Resource
    private CopcService copcService;


    /**
     * Consumer1
     * 接受payed_queue1的消息
     * @param message
     */
    @RabbitListener(queues = SysConstant.COPC_ISV_PAYED_QUEUE1)
    @RabbitHandler
    public void listenSendToManyConsumerPayedQueue1(Message message, Channel channel, String msg) throws IOException {
        log.info("---------->PAYED订单Consumer1接收到COPC_ISV_QUEUE的消息:{}",message.toString());
        copcService.saveOrder(msg);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    /**
     * Consumer2
     * 接受payed_queue1的消息
     * @param message
     */
    @RabbitListener(queues = SysConstant.COPC_ISV_PAYED_QUEUE1)
    @RabbitHandler
    public void listenSendToManyConsumerPayedQueue2(Message message, Channel channel, String msg) throws IOException {
        log.info("---------->PAYED订单Consumer2接收到COPC_ISV_QUEUE的消息:{}",message.toString());
        copcService.saveOrder(msg);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    /**
     * Consumer3
     * 接受payed_queue1的消息
     * @param message
     */
    @RabbitListener(queues = SysConstant.COPC_ISV_PAYED_QUEUE1)
    @RabbitHandler
    public void listenSendToManyConsumerPayedQueue3(Message message, Channel channel, String msg) throws IOException {
        log.info("---------->PAYED订单Consumer3接收到COPC_ISV_QUEUE的消息:{}",message.toString());
        copcService.saveOrder(msg);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


    /**
     * Consumer1
     * 接受cancel_queue1的消息
     * @param message
     */
    @RabbitListener(queues = SysConstant.COPC_ISV_CANCEL_QUEUE1)
    @RabbitHandler
    public void listenSendToManyConsumerCancelQueue1(Message message, Channel channel, String msg) throws IOException {
        log.info("---------->CANCEL订单Consumer1接收到COPC_ISV_QUEUE的消息:{}",message.toString());
        copcService.cancelOrder(msg);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    /**
     * Consumer2
     * 接受cancel_queue1的消息
     * @param message
     */
    @RabbitListener(queues = SysConstant.COPC_ISV_CANCEL_QUEUE1)
    @RabbitHandler
    public void listenSendToManyConsumerCancelQueue2(Message message, Channel channel, String msg) throws IOException {
        log.info("---------->CANCEL订单Consumer2接收到COPC_ISV_QUEUE的消息:{}",message.toString());
        copcService.cancelOrder(msg);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


    /**
     * Consumer3
     * 接受cancel_queue1的消息
     * @param message
     */
    @RabbitListener(queues = SysConstant.COPC_ISV_CANCEL_QUEUE1)
    @RabbitHandler
    public void listenSendToManyConsumerCancelQueue3(Message message, Channel channel, String msg) throws IOException {
        log.info("---------->CANCEL订单Consumer3接收到COPC_ISV_QUEUE的消息:{}",message.toString());
        copcService.cancelOrder(msg);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


    /**
     * Consumer1
     * 接受voucher_queue1的消息
     * @param message
     */
    @RabbitListener(queues = SysConstant.COPC_ISV_VOUCHER_QUEUE1)
    @RabbitHandler
    public void listenSendToManyConsumerVoucherQueue1(Message message, Channel channel, String msg) throws IOException {
        log.info("---------->VOUCHER订单Consumer1接收到COPC_ISV_QUEUE的消息:{}",message.toString());
        copcService.voucherVerification(msg);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    /**
     * Consumer2
     * 接受voucher_queue1的消息
     * @param message
     */
    @RabbitListener(queues = SysConstant.COPC_ISV_VOUCHER_QUEUE1)
    @RabbitHandler
    public void listenSendToManyConsumerVoucherQueue2(Message message, Channel channel, String msg) throws IOException {
        log.info("---------->VOUCHER订单Consumer2接收到COPC_ISV_QUEUE的消息:{}",message.toString());
        copcService.voucherVerification(msg);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


    /**
     * Consumer3
     * 接受voucher_queue1的消息
     * @param message
     */
    @RabbitListener(queues = SysConstant.COPC_ISV_VOUCHER_QUEUE1)
    @RabbitHandler
    public void listenSendToManyConsumerVoucherQueue3(Message message, Channel channel, String msg) throws IOException {
        log.info("---------->VOUCHER订单Consumer3接收到COPC_ISV_QUEUE的消息:{}",message.toString());
        copcService.voucherVerification(msg);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
